package com.lxitedu.server;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;

public class ClientWithResponseHandler {

  public final static void main(String[] args) throws Exception {
    String responseBody = getResponseBody("http://www.csdn.com");
    System.out.println("-----------------------------------");
    String[] urls = StringUtils
        .substringsBetween(responseBody, "href=\"", "\"");
    System.out.println(Arrays.toString(urls));
    System.out.println("-----------------------------------");
    String[] contents = StringUtils.substringsBetween(responseBody, ">", "<");
    List<String> emails = new ArrayList<String>();
    for (String content : contents) {
      if (content.matches("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*")) {
        emails.add(content);
      }
    }
    System.out.println(Arrays.toString(emails.toArray()));
  }

  public static String getResponseBody(String url) throws IOException,
      ClientProtocolException {
    HttpClient httpclient = new DefaultHttpClient();
    HttpGet httpget = new HttpGet(url);
    System.out.println("executing request " + httpget.getURI());
    ResponseHandler<String> responseHandler = new BasicResponseHandler();
    String responseBody = httpclient.execute(httpget, responseHandler);
    System.out.println(responseBody);
    httpclient.getConnectionManager().shutdown();
    return responseBody;
  }

}
